using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace TreeListViewDemo{ public partial class Form1 : Form { private void AddTreeViewNodes() { // 添加根节点 TreeNode tn = new TreeNode(); tn.Text = "金庸小说"; tn.ImageIndex = 0; tn.SelectedImageIndex = 0; tv.Nodes.Add((TreeNode)(tn.Clone())); // 添加4个子节点 tn.Text = "射雕英雄传"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[0].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "神雕侠侣"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[0].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "天龙八部"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[0].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "倚天屠龙记"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[0].Nodes.Add((TreeNode)(tn.Clone())); // 添加根节点 tn.Text = "古龙小说"; tn.ImageIndex = 0; tn.SelectedImageIndex = 0; tv.Nodes.Add((TreeNode)(tn.Clone())); // 添加根节点 tn.Text = "黄易小说"; tn.ImageIndex = 0; tn.SelectedImageIndex = 0; tv.Nodes.Add((TreeNode)(tn.Clone())); // 添加3个子节点 tn.Text = "大唐双龙传"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[2].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "寻秦记"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[2].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "第1章 时空机器"; tn.ImageIndex = 3; tn.SelectedImageIndex = 4; tv.Nodes[2].Nodes[1].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "第2章 墨子剑法"; tn.ImageIndex = 3; tn.SelectedImageIndex = 4; tv.Nodes[2].Nodes[1].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "第3章 初会秦皇"; tn.ImageIndex = 3; tn.SelectedImageIndex = 4; tv.Nodes[2].Nodes[1].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "第4章 一统天下"; tn.ImageIndex = 3; tn.SelectedImageIndex = 4; tv.Nodes[2].Nodes[1].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "翻云覆雨"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[2].Nodes.Add((TreeNode)(tn.Clone())); } public Form1() { InitializeComponent(); // 调用向树视图中添加节点的方法 AddTreeViewNodes(); } private void contextMenuOfTv_Opening(object sender, CancelEventArgs e) { } private void tv_AfterSelect(object sender, TreeViewEventArgs e) { // 在文本框中显示当前选择节点的完全路径 txtPath.Text = tv.SelectedNode.FullPath; // 在列表视图中显示当前选择节点的所有子节点 lv.Clear();// 首先清除列表视图的所有列和项 // 向列表视图中添加列,当前选择节点不同,列的标题 // 也不同 if (tv.SelectedNode.ImageIndex == 0) { lv.Columns.Add("书名", 160, HorizontalAlignment.Left); lv.Columns.Add("页数", 60, HorizontalAlignment.Right); lv.Columns.Add("价格", 60, HorizontalAlignment.Right); } else { lv.Columns.Add("标题", 160, HorizontalAlignment.Left); lv.Columns.Add("字数", 60, HorizontalAlignment.Right); lv.Columns.Add("页数", 60, HorizontalAlignment.Right); } int i = 300, j = 20; int imgIndex = 0; if (tv.SelectedNode.ImageIndex == 1) imgIndex = 1; // 向列表视图中添加项 foreach (TreeNode tn in tv.SelectedNode.Nodes) { ListViewItem lvi = new ListViewItem(tn.Text, imgIndex); lvi.SubItems.Add(i.ToString()); lvi.SubItems.Add(j.ToString()); lv.Items.Add(lvi); i = 100; j = 10; } } private void lv_SelectedIndexChanged(object sender, EventArgs e) { // 把当前项的标题附加到树视图当前选择节点的完全路径的 // 后面并显示在文本框中 if (lv.SelectedItems.Count > 0) txtPath.Text = tv.SelectedNode.FullPath tv.PathSeparator lv.SelectedItems[0].Text; } private void miLargeIcon_Click(object sender, EventArgs e) { miLargeIcon.Checked = true; miSmallIcon.Checked = false; miList.Checked = false; miDetail.Checked = false; lv.View = System.Windows.Forms.View.LargeIcon; } private void miSmallIcon_Click(object sender, EventArgs e) { miLargeIcon.Checked = false; miSmallIcon.Checked = true; miList.Checked = false; miDetail.Checked = false; lv.View = System.Windows.Forms.View.SmallIcon; } private void miList_Click(object sender, EventArgs e) { miLargeIcon.Checked = false; miSmallIcon.Checked = false; miList.Checked = true; miDetail.Checked = false; lv.View = System.Windows.Forms.View.List; } private void miDetail_Click(object sender, EventArgs e) { miLargeIcon.Checked = false; miSmallIcon.Checked = false; miList.Checked = false; miDetail.Checked = true; lv.View = System.Windows.Forms.View.Details; } private void miNewNode_Click(object sender, EventArgs e) { TreeNode tn = new TreeNode("新建节点"); if (tv.SelectedNode.ImageIndex == 0) { tn.ImageIndex = 1; tn.SelectedImageIndex = 2; } else { tn.ImageIndex = 3; tn.SelectedImageIndex = 4; } tv.SelectedNode.Nodes.Add(tn); } private void miDeleteNode_Click(object sender, EventArgs e) { tv.SelectedNode.Remove(); } private void miExpandAll_Click(object sender, EventArgs e) { tv.ExpandAll(); } private void miCollapseAll_Click(object sender, EventArgs e) { tv.CollapseAll(); } private void miChangeSep_Click(object sender, EventArgs e) { if (tv.PathSeparator == "\\") tv.PathSeparator = "|"; else tv.PathSeparator = "\\"; } private void miShowPlusMinus_Click(object sender, EventArgs e) { miShowPlusMinus.Checked =! miShowPlusMinus.Checked; tv.ShowPlusMinus = miShowPlusMinus.Checked; } private void miShowLine_Click(object sender, EventArgs e) { miShowLine.Checked =! miShowLine.Checked ; tv.ShowLines = miShowLine.Checked; } }}
下载c# 经典TreeListView方法 实例源码下载用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论